﻿using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace JsonCodeGenerator
{
    /// <summary>
    /// 属性名修改
    /// </summary>
    internal class PropertyNameRewriter : CSharpSyntaxRewriter
    {
        private readonly string _originalPropertyName;
        private readonly string _newPropertyName;

        public PropertyNameRewriter(string originalPropertyName, string newPropertyName)
        {
            _originalPropertyName = originalPropertyName;
            _newPropertyName = newPropertyName;
        }

        public override SyntaxNode? VisitPropertyDeclaration(PropertyDeclarationSyntax node)
        {
            if (node.Identifier.ValueText == _originalPropertyName)
            {
                // 创建新的属性名标识符
                var newIdentifier = SyntaxFactory.Identifier(_newPropertyName);

                // 替换原有的属性名标识符
                var updatedNode = node.WithIdentifier(newIdentifier);

                return updatedNode;
            }

            return base.VisitPropertyDeclaration(node);
        }
    }
}
